package comxin.stringtest;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class StringMethodTest {
    @Test
    public void method() {
        String name = "zhangsan";
        System.out.println(name.charAt(0));
        System.out.println(name.indexOf("a"));
    }

    @Test
    public void method1() {
        String name = "      xinxin       ";
        String trim = name.trim();
        System.out.println(trim);

    }

    @Test
    public void method2() {
        String name = "      .       zhangsan    .       ";
        String[] trim = name.split("s");
        trim = name.split("s", 1);

        for (String tem : trim) {
            System.out.println(tem);
        }

    }

    @Test
    public void method3() {
        String name = "zhangxin";
        String substring = name.substring(3);
        String substring1 = name.substring(3, 5);
        System.out.println(substring);
        System.out.println(substring1);
    }

    @Test
    public void method4() {
        String name = "zhangsan.png";
        int index = name.lastIndexOf(".");
        String substring = name.substring(index);
        System.out.println(index);
        System.out.println(substring);

    }

    @Test
    public void countChar() {
        String name = "zhangxinxxxxxx";
        char[] chars = name.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        boolean flag = true;
        for (char ch : chars) {
            Character character = new Character(ch);
            Set<Character> characters = map.keySet();
            for (Character temp : characters) {
                if (character.equals(temp)) {
                    Integer integer = map.get(character);
                    integer++;
                    map.put(character, integer);
                    flag = false;
                    break;
                }
                flag = true;
            }
            if (flag) {
                map.put(character, 1);
            }
        }
        System.out.println(map.toString());
    }




}
